package com.yyt.threadpooldemo.thread_pool_kinds;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 5种线程池
 */
public class SingleThreadPoolDemo {
    public static void main(String[] args) {
        // 1.单例线程池
        ExecutorService singleThreadExecutor = Executors.newSingleThreadExecutor();
        // 2.固定数量线程池
        ExecutorService fixedThreadExecutor = Executors.newFixedThreadPool(2);
        // 3.并行线程池
        ExecutorService workStealingPool = Executors.newWorkStealingPool();
        // 4.缓存线程池
        ExecutorService cachedThreadPool = Executors.newCachedThreadPool();
        // 5.延迟线程池
        ExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(1);


        for (int i = 0; i < 5; i++) {
            final int index = i;
            singleThreadExecutor.execute(() -> {
                try {
                    SimpleDateFormat sdf = new SimpleDateFormat(
                            "HH:mm:ss");
                    System.out.println("运行时间: " +
                            sdf.format(new Date()) + " " + index);
                    // 睡眠2s
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
        }
        singleThreadExecutor.shutdown();
    }
}

